#include "users.h"

int Users :: Create( ) // создаем таблицу
{
	PGresult * res = PQexec( conn, "CREATE TABLE users ( id serial NOT NULL, login character varying(32) NOT NULL UNIQUE, pass character varying(32) NOT NULL, CONSTRAINT users_pkey PRIMARY KEY ( id ) );" );
	int result = PQresultStatus( res );
	if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
		result = DB_OK;
	else
		result = DB_NO_RESULT;
	PQclear( res );
	return result;
}

int Users :: Clean( ) // очищаем таблицу
{
	PGresult * res = PQexec( conn, "DROP TABLE users;" );
	//			cout << PQresultStatus( res ) << endl;
	int result = PQresultStatus( res );
	if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
		result = DB_OK;
	else
		result = DB_NO_RESULT;
	PQclear( res );
	return result;
}

int Users :: FindRecord( char * login, char * pass, int & id ) // проверка логина и пароля +
{
	char * st1;
	ExtraClass :: GetString( st1, "SELECT id FROM users WHERE login = '", login, "' AND pass = '", pass, "' LIMIT 1;" );

	PGresult * res = PQexec( conn, st1 );
	int result = PQresultStatus( res );
	//			cout << st1 << endl;
	if ( PQresultStatus( res ) == PGRES_TUPLES_OK )
	{
		if ( PQntuples( res ) )
		{
			result = DB_OK;
			char * endptr;
			id = strtol( PQgetvalue( res, 0, 0 ), &endptr, 10 ); // не проверяем.
		}
		else
		{
			result = DB_NO_RECORD;
			id = -1;
		}
	}
	else
		result = DB_NO_RESULT;
	PQclear( res );

	//			cout << "FindRecord( ) id = " << id << endl;

	return result;
}

int Users :: FindRecord( char * login, int & id ) // проверка на существование логина +
{
	char * st1;
	ExtraClass :: GetString( st1, "SELECT id FROM users WHERE login = '", login, "' LIMIT 1;" );

	PGresult * res = PQexec( conn, st1 );
	int result = PQresultStatus( res );
	if ( PQresultStatus( res ) == PGRES_TUPLES_OK )
	{
		if ( PQntuples( res ) )
		{
			result = DB_OK;
			char * endptr;
			id = strtol( PQgetvalue( res, 0, 0 ), &endptr, 10 ); // не проверяем.
		}
		else
		{
			result = DB_NO_RECORD;
			id = -1;
		}
	}
	else
		result = DB_NO_RESULT;
	PQclear( res ); // ???
	return result;
}

int Users :: InsRecord( char * login, char * pass ) // добавление новой записи +
{
	//struct timeval start, end; // счет времени.

	//long mtime, seconds, useconds;
	//gettimeofday(&start, NULL);


	int id;
	int status = FindRecord( login, id );
	int result;

//	cout << "InsRecord( ) status = " << status << endl;

	if ( status == DB_NO_RECORD )
	{

		char * st1;
		ExtraClass :: GetString( st1, "INSERT INTO users (login,pass) VALUES ('", login,  "','", pass, "');" );

//		cout << "InsRecord( ) st1 = " << st1 << endl;

		PGresult * res = PQexec( conn, st1 );
		if ( PQresultStatus( res ) == PGRES_COMMAND_OK )
			result = DB_OK;
		else
			result = DB_NO_RESULT;
		PQclear( res );
	}
	else
		result = DB_NO_INSERT;

	//gettimeofday(&end, NULL);

	//seconds  = end.tv_sec  - start.tv_sec;
	//useconds = end.tv_usec - start.tv_usec;

	//mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
	//			cout << "\ttime " << mtime << endl;

	return result;
}
